package com.andyage.admin.action;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.andyage.util.RandomCode;
/**
 * 验证码请求
 * @author john
 *
 */
@Controller 
@RequestMapping("/random/code")  
public class RandomCodeAction {
	@RequestMapping("/generate.do")    
	public void generateCode(HttpServletResponse response) { 
		// 验证码图片宽度，单位像素
		int width = 120;
		// 验证码图片高度，单位像素  
		int height = 30;     
		// 验证码图片格式      
		String format = "png";   
		// 验证码字符长度      
		int len = 4;     
		// 设置图片格式     
		response.setContentType("image/" + format);
		// 禁止浏览器缓存图片 
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");  
		response.setDateHeader("Expires", 0);       
		String code = RandomCode.randomString(len);    
		// 把图片输出到response输出流  
		try {     
			RandomCode.write(code, width, height, response.getOutputStream(), format);
			} catch (IOException e) {   
				e.printStackTrace();   
				}  
		}  
	}
